package com.baiyang.server.redis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baiyang.server.dao.system.UserRoleDao;
import com.baiyang.server.tools.DataTool;

/**
 * @apiNote 用户角色信息
 * @author XVX
 */
@Component
public class UserRoleRedis {
	private final static String USER_ROLE = "USER_ROLE";
	@Autowired
	private RedisUtil redisUtil = null;
	@Autowired
	private UserRoleDao userRoleDao = null;
	
	/**
	 * @apiNote 根据用户id获取用户角色id
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getRoleIdsByUserId(String userId){
		if(DataTool.isBlank(userId)) {
			return null;
		}
		List<String> roleIds = (List<String>) redisUtil.hget(USER_ROLE, userId);
		if(DataTool.isBlank(roleIds)) {
			roleIds = userRoleDao.selectIdsByUserId(userId);
			if(!DataTool.isBlank(roleIds)) {
				redisUtil.hset(USER_ROLE, userId, roleIds);
			}
		}
		return roleIds;
	}
	
	/**
	 * @apiNote 根绝用户id删除管理角色ids
	 * @param id
	 */
	public void deleteById(String userId) {
		redisUtil.hdel(USER_ROLE, userId);
	}
	
	
}
